package com.brainhealth.customer.service.impl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.Hutool;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.brainhealth.common.config.RuoYiConfig;
import com.brainhealth.common.core.domain.TreeSelect;
import com.brainhealth.common.utils.AesUtils;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.common.utils.file.FileUploadUtils;
import com.brainhealth.utils.CommonUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import org.hibernate.validator.internal.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.MechanismMapper;
import com.brainhealth.customer.domain.Mechanism;
import com.brainhealth.customer.service.IMechanismService;

import javax.imageio.ImageIO;

/**
 * 机构信息Service业务层处理
 * 
 * @author wolong
 * @date 2023-07-26
 */
@Service
public class MechanismServiceImpl implements IMechanismService 
{
    private static final Logger log = LoggerFactory.getLogger(MechanismServiceImpl.class);

    @Autowired
    private MechanismMapper mechanismMapper;

    /**
     * 查询机构信息
     * 
     * @param mechanismId 机构信息主键
     * @return 机构信息
     */
    @Override
    public Mechanism selectMechanismByMechanismId(Long mechanismId)
    {
        return mechanismMapper.selectMechanismByMechanismId(mechanismId);
    }

    /**
     * 查询机构信息列表
     * 
     * @param mechanism 机构信息
     * @return 机构信息
     */
    @Override
    public List<Mechanism> selectMechanismList(Mechanism mechanism)
    {
        Mechanism query = new Mechanism();
        List<Mechanism> lstMechanism = mechanismMapper.selectMechanismList(query);
        List<Mechanism> lstTree = new ArrayList<>();
        for (Mechanism root : lstMechanism)
        {
            if (root.getParentId().equals(0l))
            {
                getMechanismTree(root, lstMechanism);
                lstTree.add(root);
            }
        }
        return lstTree;
    }

    @Override
    public List<Mechanism> selectMechanismCollect(Mechanism mechanism) {
        Mechanism query = new Mechanism();
        List<Mechanism> lstMechanism = mechanismMapper.selectMechanismList(query);
        return lstMechanism;
    }

    /**
     * 构造机构信息树形结构
     * @param node
     * @param lstData
     */
    public void getMechanismTree(Mechanism node, List<Mechanism> lstData)
    {
        List<Mechanism> lstChild = lstData.stream().filter(item -> item.getParentId() != null && item.getParentId().equals(node.getMechanismId())).collect(Collectors.toList());
        for (Mechanism child : lstChild)
        {
            node.getChildren().add(child);
            getMechanismTree(child, lstData);
        }
    }

    /**
     * 新增机构信息
     * 
     * @param mechanism 机构信息
     * @return 结果
     */
    @Override
    public int insertMechanism(Mechanism mechanism)
    {
        mechanism.setCreateTime(DateUtils.getNowDate());
        return mechanismMapper.insertMechanism(mechanism);
    }

    public String LINK_URL= "https://www.ainaoxingdong.com/brain/index.html#/detailPage?mechanismId=";

    @Override
    public String createQrCode(Long mechanismId)
    {
        Mechanism mechanism = mechanismMapper.selectMechanismByMechanismId(mechanismId);
        String mechanismInfo = AesUtils.encryptHex(mechanism.getMechanismId().toString());
        String filePath = RuoYiConfig.getUploadPath();
        try
        {
            String fileName = FileUploadUtils.generateCode(LINK_URL + mechanismInfo, filePath, mechanism.getMechanismName());
            File f = new File(fileName);
            String uploadFile = FileUploadUtils.upload(filePath, FileUploadUtils.getMultipartFile(f));
            mechanism.setShareUrl(LINK_URL + mechanismInfo);
            mechanism.setQrCode(uploadFile);
            updateMechanism(mechanism);
            return uploadFile;
        } catch (Exception e)
        {
            log.error("创建二维码失败！", e);
            return "";
        }
    }

    @Override
    public List<TreeSelect> getMechanismTree()
    {
        Mechanism queryMechanism = new Mechanism();
        List<Mechanism> lstData = mechanismMapper.selectMechanismList(queryMechanism);
        List<TreeSelect> lstMechanismTree = new ArrayList<>();
        for (Mechanism mechanism : lstData)
        {
            if (mechanism.getParentId() == 0)
            {
                TreeSelect root = new TreeSelect();
                root.setId(mechanism.getMechanismId());
                root.setLabel(mechanism.getMechanismName());
                lstMechanismTree.add(root);
                CommonUtils.getTreeSelectChildren(root, lstData,"getMechanismId","getMechanismName","getParentId");
            }
        }
        return lstMechanismTree;
    }

    /**
     * 修改机构信息
     * 
     * @param mechanism 机构信息
     * @return 结果
     */
    @Override
    public int updateMechanism(Mechanism mechanism)
    {
        mechanism.setUpdateTime(DateUtils.getNowDate());
        return mechanismMapper.updateMechanism(mechanism);
    }

    /**
     * 批量删除机构信息
     * 
     * @param mechanismIds 需要删除的机构信息主键
     * @return 结果
     */
    @Override
    public int deleteMechanismByMechanismIds(Long[] mechanismIds)
    {
        return mechanismMapper.deleteMechanismByMechanismIds(mechanismIds);
    }

    /**
     * 删除机构信息信息
     * 
     * @param mechanismId 机构信息主键
     * @return 结果
     */
    @Override
    public int deleteMechanismByMechanismId(Long mechanismId)
    {
        return mechanismMapper.deleteMechanismByMechanismId(mechanismId);
    }
}
